package com.foreknow.view;

import com.foreknow.bean.Guest;
import com.foreknow.service.GuestService;
import com.foreknow.service.impl.GuestServiceImpl;
import com.foreknow.util.DataUtil;
import com.foreknow.util.PropertyJTable;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author xiaoming
 * @version jdk1.8
 * @date 2022/6/28 11:44
 * @function:
 */
public class MainWindow extends JFrame {

    private JPanel contentPane;
    String columnName[]={"id","name","phone","email","title","content","createdtime"};
    JTable jTable;
    PropertyJTable propertyJTable = PropertyJTable.getjTable();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    //MainWindow frame = new MainWindow();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainWindow() {

        setTitle("\u4E3B\u7A97\u53E3");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        JButton button = new JButton("select");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("");
            }
        });

        JTextField textField = new JTextField();
        textField.setBounds(30, 30, 93, 23);
        contentPane.add(textField);
        textField.setColumns(10);
        button.setBounds(150, 30, 93, 23);
        contentPane.add(button);
        JButton button2 = new JButton("insert");
        button2.setBounds(150, 30, 93, 23);
        contentPane.add(button2);
        JButton button3 = new JButton("update");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取到选中的行
                int row = jTable.getSelectedRow();
                Integer id = (Integer) jTable.getValueAt(row,0);
                GuestService service = new GuestServiceImpl() ;
                Guest guest = service.getQueryById(id);
                String name = (String) jTable.getValueAt(row,1);
                String phone = (String) jTable.getValueAt(row,2);
                String email = (String) jTable.getValueAt(row,3);
                String title = (String) jTable.getValueAt(row,4);
                String content = (String) jTable.getValueAt(row,5);
                String createdTime = (String) jTable.getValueAt(row,6);
                guest.setName(name);
                guest.setPhone(phone);
                guest.setEmail(email);
                guest.setTitle(title);
                guest.setContent(content);
                guest.setCreatedTime(createdTime);
                service.update(guest);
            }
        });
        contentPane.add(button3);
        button3.setBounds(150, 30, 93, 23);
        JButton button4 = new JButton("delete");
        button4.setBounds(150, 30, 93, 23);
        contentPane.add(button4);

        GuestService service = new  GuestServiceImpl();
        List<Object> list = service.getQueryAll();
        Object[][] data = DataUtil.convert(list);
        jTable = propertyJTable.createJtable(data,columnName);
        jTable.setVisible(true);
        contentPane.add(new JScrollPane(jTable));
        this.setVisible(true);
    }
}